<template>
<div>	
	<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;margin-top:0px;margin-bottom:5px;">
			<el-form :inline="true">
			  	<el-col :span="24">
					<el-form-item lable="条款代码" style="width: 10%;">
						<el-input v-model="formData.kindcode" size="small" placeholder="条款代码"></el-input>
					</el-form-item>
					<!-- <el-form-item lable="险种代码" label-width="150px" :span="3">
						<el-input v-model="riskCodesLabel" size="small" placeholder="险种代码" readonly @click.native="openRiskCodeSelectDialog()"></el-input>
					</el-form-item> -->
					<el-form-item lable="版本号" style="width: 6%;">
						<el-input v-model="formData.kindversion" size="small" placeholder="版本号"></el-input>
					</el-form-item>
					<el-form-item lable="条款中文名称">
						<el-input v-model="formData.kindcname" size="small" placeholder="条款中文名称"></el-input>
					</el-form-item>
					<!-- <el-form-item lable="险别英文名称" label-width="150px" :span="3">
						<el-input v-model="formData.kindename" size="small" placeholder="险别英文名称"></el-input>
					</el-form-item> -->
					<el-form-item lable="创建日期">
						<el-date-picker type="date" placeholder="创建日期" v-model="formData.createdate" size="small"></el-date-picker>
					</el-form-item>
					<el-form-item lable="是否有效" style="width: 10%;">
						<el-select v-model="formData.validstatus" placeholder="是否有效" size="small">
							<el-option v-for="item in validOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button size="small" type="primary" @click="queryData()" icon="search">查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button size="small" @click="reset()"><i class="fa fa-undo"></i> 重置</el-button>
					</el-form-item>
			  	</el-col>
			</el-form>
		</el-col>

		<el-table :data="tableData" v-loading="loading" element-loading-text="处理中......" ref="multipleTable" border style="width: 100%;" :height="tableHeight" :highlight-current-row="true" @selection-change="rowSelected" @sort-change="orderBy" @row-dblclick="showDetail">
		 	<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="kindcode" label="条款代码" sortable="custom" width="140"></el-table-column>
			<el-table-column prop="kindcname" label="条款名称" sortable="custom" show-overflow-tooltip min-width="150"></el-table-column>
			<el-table-column prop="kindversion" label="条款版本" width="120"></el-table-column>
			<el-table-column prop="startdate" label="生效日期" sortable="custom" width="150" :formatter="dateFormater"></el-table-column>
			<el-table-column prop="enddate" label="失效日期" sortable="custom" width="150" :formatter="dateFormater"></el-table-column>
			<el-table-column prop="validstatus" label="是否有效" sortable="custom" width="120" :formatter="validFormater"></el-table-column>
			<el-table-column label="操作" width="180" fixed="right">
				<template scope="scope">
					<el-button size="small" type="text"  @click="showRiskLinked(scope.row)">已关联险种</el-button>
					<el-button size="small" type="text" @click="showItemLinked(scope.row)">已关联责任</el-button>
				</template>
			</el-table-column>
		</el-table>


		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;margin-top:0px;margin-bottom:5px;">
			<el-form :inline="true">
				<el-col :span="8">
					<el-col :span="24">
						<el-form-item>
							<el-button size="small" type="warning" @click="openAddDialog()" icon="plus">新增</el-button>
						</el-form-item>
						<el-form-item>
							<el-button size="small" type="danger" @click="deleteData()" icon="delete">注销</el-button>
						</el-form-item>
						<el-form-item>
							<el-button size="small" type="primary" @click="openEditDialog()" icon="edit">修改</el-button>
						</el-form-item>
						<el-form-item>
							<el-button size="small" type="default" @click="openLogDialog()"><i class="fa fa-search-plus"></i> 查看日志</el-button>
						</el-form-item>
						<el-form-item>
							<el-button size="small" type="warning" @click="linkRisk()"><i class="fa fa-link"></i> 关联【险种】</el-button>
						</el-form-item>
						<el-form-item>
							<el-button size="small" type="warning" @click="linkItem()"><i class="fa fa-link"></i> 关联【责任】</el-button>
						</el-form-item>
						<!-- <el-form-item>
							<el-button size="small" type="info" @click="openRiskLinkItemDialog()"><i class="fa fa-gear"></i> 险种配置</el-button>
						</el-form-item> -->
						<el-form-item>
							<el-button size="small" type="warning" @click="openEfileAddDialog()"><i class="fa fa-plus-square-o"></i> 配置备案号</el-button>
						</el-form-item>
					</el-col>
				</el-col>
				<el-col :span="16">
					<div class="block" style="float:right">
						<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
						      :current-page="formData.currentPage"
						      :page-sizes="[15, 30, 45]"
						      :page-size="formData.pageSize"
						      layout="total, sizes, prev, pager, next, jumper"
						      :total="total">
						    </el-pagination>
					</div>
				</el-col>
			</el-form>
		</el-col>

		<KindEdit v-if="showEditDialog"></KindEdit>
		<KindAdd v-if="showAddDialog"></KindAdd>
		<KindDetail v-if="showDetailDialog"></KindDetail>
		<LinkItem v-if="showItemConfigDialog"></LinkItem>
		<LinkRisk v-if="showRiskConfigDialog"></LinkRisk>
		<RiskCodeSelect v-if="showRiskCodeSelectDialog"></RiskCodeSelect>
		<ItemLinked v-if="showItemLinkedDialog"></ItemLinked>
		<RiskLinked v-if="showRiskLinkedDialog"></RiskLinked>
		<Log v-if="showLogDialog"></Log>
		<RiskLinkItem v-if="showRiskLinkItemDialog"></RiskLinkItem>
		<EFileAdd v-if="showEFileAddDialog"></EFileAdd>

</div>
</template>

<script>
import KindAPI from "../../../js/production/kind/kindAPI";
import dateUtil from "../../../common/js/dateUtil";
import Utils from"../../../js/production/utils";
import KindEdit from "./dialog/KindEdit";
import KindAdd from "./dialog/KindAdd";
import RiskCodeSelect from "../common/RiskCodeSelect";
import KindDetail from "./dialog/KindDetail";
import LinkItem from "./dialog/LinkItem";
import LinkRisk from "./dialog/LinkRisk";
import ItemLinked from "./dialog/ItemLinked";
import RiskLinked from "./dialog/RiskLinked";
import Log from "../common/Log";
import RiskLinkItem from "./dialog/RiskLinkItem";
import EFileAdd from "../efile/dialog/EFileAdd";
export default {
  components : {
  	KindEdit,
  	KindAdd,
  	KindDetail,
  	LinkItem,
  	LinkRisk,
  	RiskCodeSelect,
  	ItemLinked,
  	RiskLinked,
  	Log,
  	RiskLinkItem,
  	EFileAdd
  },
  data () {
    	return {
    		formData: {
    		        currentPage: 1,
	                    pageSize: 15,
	                    kindcode: '',
	                    kindversion: '',
	                    kindcname: '',
	                    kindename: '',
	                    createdate: undefined,
	                    riskcodes: {},
	                    validstatus: '',
	                    orderBy: '',
	                    order: '',
    		},
    		riskCodesLabel: '',
	            arrayRiskcodes: [],
    		tableData: [],
    		selectedRows: [],
    		total: 0,
		riskOptions: [],
		validOptions: [{
			label : '1-有效',
			value: '1'
		},{
			label : '0-无效',
			value: '0'
		}],
		isSortRequest: false,	//是否请求 
		showEditDialog: false,
		showAddDialog: false,
		showDetailDialog: false,
		showItemConfigDialog: false,
		showRiskConfigDialog: false,
		showRiskCodeSelectDialog: false,
		showItemLinkedDialog: false,
		showRiskLinkedDialog: false,
		showLogDialog: false,
		showRiskLinkItemDialog: false,
		showEFileAddDialog : false,
		rowDetail: {},
		tableHeight: 0,
		logBusinessKey: '',
		logBusinessKeyValue: '',
		loading : false,
		tempKind : {},
	}
  },
  mounted() {
  	//KindAPI.initRiskSelect(this);
  	this.tableHeight = Utils.getScreenHeight()*0.72;
  	let vm = this;
  	document.onkeydown = function(e) {
  		if(e.keyCode == 13) {
  			vm.queryData();
  		}
  	}
  },
  methods: {
		queryData() {
			if (this.formData.createdate === '') {
				this.formData.createdate = undefined;
			}
			KindAPI.query(this);
		},
		deleteData() {
			KindAPI.delete(this);
		},
		openEditDialog() {
			KindAPI.openEditDialog(this);
		},
		openAddDialog() {
			KindAPI.openAddDialog(this);
		},
		openRiskCodeSelectDialog() {
			KindAPI.openRiskCodeSelectDialog(this);
		},
		openEfileAddDialog() {
			for(let i = 0; i < this.selectedRows.length; i++) {
				if(this.selectedRows[i].validstatus == '0') {
					this.$message({type: 'error',message: '请选择有效条款！'});
					return;
				}
			}
			this.showEFileAddDialog = true;
		},
		rowSelected(rows) {
			this.selectedRows = rows;
		},
		handleSizeChange(val) {
			this.formData.pageSize = val;
			KindAPI.query(this);
		},
		handleCurrentChange(val) {
			this.formData.currentPage = val;
			KindAPI.query(this);
		},
		dateFormater(row,  column) {  
			var  date  =  row[column.property];            
			if  (date  ==  undefined)  {               
				return  "";            
			}  
			return dateUtil.getDateyMd(date);
		},
		validFormater(row,  column) {  
			var  valid =  row[column.property];            
			if  (valid ==  undefined)  {               
				return  "";            
			}  
			if (valid == 1) {
				return "有效";
			}
			if (valid == 0) {
				return "无效";
			}
		},
		reset() {
			this.formData.kindcode = '';
			this.arrayRiskcodes = [];
			this.riskCodesLabel = '';
			this.formData.kindversion = '';
			this.formData.kindcname = '';
			this.formData.kindename = '';
			this.formData.createdate = undefined;
			this.formData.validstatus = '';
			this.formData.currentPage = 1;
			KindAPI.query(this);
		},
		orderBy(column, prop, order) {
			if (typeof(column) != 'undefined') {
				this.formData.orderBy = column.prop;
				if (column.order == 'descending') {
					this.formData.order = 'desc';
				} else {
					this.formData.order = 'asc';
				}
			}
			if (this.isSortRequest) {
				this.isSortRequest = false;
				KindAPI.query(this);
			}
		},
		showDetail(row, event) {
			this.rowDetail = row;
			this.showDetailDialog = true;
		},
		rowSelect(row) {
			this.$refs.multipleTable.toggleRowSelection(row);
		},
		linkItem() {
			KindAPI.openItemConfigDialog(this);
		},
		linkRisk() {
			KindAPI.openRiskConfigDialog(this);
		},
		showItemLinked(row) {
			this.tempKind = row;
			this.selectedRows = [];
			this.selectedRows.push(row);
			KindAPI.openItemLinkedDialog(this);
			this.queryData();
		},
		showRiskLinked(row) {
			this.tempKind = row;
			this.selectedRows = [];
			this.selectedRows.push(row);
			KindAPI.openRiskLinkedDialog(this);
			this.queryData();
		},
		openLogDialog() {
			KindAPI.openLogDialog(this);
		},
		openRiskLinkItemDialog() {
			KindAPI.openRiskLinkItemDialog(this);
		}
  }
}
</script>